package com.yyx.app1.Data.Medication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yyx.app1.Data.DataInit;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yuexing on 2015/9/29.
 */
public class MedicationSecondList_SQLiteOperation {
    private DataInit dataInit;
    private String TableName;

    public MedicationSecondList_SQLiteOperation(DataInit dataInit) {
        this.dataInit = dataInit;
        TableName=dataInit.getDB_MedicationSecondListTableName();
    }

    public JSONObject getBy_Secondcolumns(String fcname) {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        JSONObject jsonObject=new JSONObject();
        try
        {
            db =dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, null, "fcname=?", new String[]{fcname}, null, null, null, null);
            if (cursor.moveToNext())
            {
                jsonObject.put("id",cursor.getString(cursor.getColumnIndex("id")));    //private String id;
                jsonObject.put("firstColumnId",cursor.getLong(cursor.getColumnIndex("firstColumnId")));    //private Long firstColumnId;    //关联一级栏目
                jsonObject.put("scname",cursor.getString(cursor.getColumnIndex("scname")));    //private String scname;         //栏目名称
                jsonObject.put("scdescription",cursor.getString(cursor.getColumnIndex("scdescription")));    //private String scdescription;  //SEO描述
                jsonObject.put("seoTitle",cursor.getString(cursor.getColumnIndex("seoTitle")));    //private String seoTitle;       //SEO标题
                jsonObject.put("sckeyWord",cursor.getString(cursor.getColumnIndex("sckeyWord")));    //private String sckeyWord;      //SEO关键字
                jsonObject.put("addTime",cursor.getString(cursor.getColumnIndex("addTime")));    //private String addTime;     //创建时间
                jsonObject.put("orderby",cursor.getInt(cursor.getColumnIndex("orderby")));    //private Integer orderby;       //排序顺序
                jsonObject.put("modulesId",cursor.getInt(cursor.getColumnIndex("modulesId")));    //private Integer modulesId;     //栏目所属模块
                jsonObject.put("url",cursor.getString(cursor.getColumnIndex("url")));    //private String url;            //路径
                jsonObject.put("urlrewrite",cursor.getString(cursor.getColumnIndex("urlrewrite")));    //private String urlrewrite;     //真实路径
                jsonObject.put("rolePower",cursor.getString(cursor.getColumnIndex("rolePower")));    //private String rolePower;     //角色权限:1表示普通用户；2表示医生
                jsonObject.put("firstColumnName",cursor.getString(cursor.getColumnIndex("firstColumnName")));    //private String firstColumnName;//栏目名称
            }
        }
        catch (Exception e)
        {
           Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            cursor.close();
            return jsonObject;
        }
    }

    public boolean find(String scname) {
        boolean result=false;
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            Cursor cursor = db.query(TableName, null, "scname=?", new String[]{scname}, null, null, null);
            result = cursor.moveToNext();
            cursor.close();
        }
        catch (Exception e)
        {
           Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            return result;
        }
    }

    public long add(Secondcolumns secondcolumns,SQLiteDatabase db) {
        long result = 0;
        try {
            ContentValues values = new ContentValues();
            values.put("id", secondcolumns.getId());//private String id;
            values.put("firstColumnId", secondcolumns.getFirstColumnId());//private Long firstColumnId;    //关联一级栏目
            values.put("scname", secondcolumns.getScname());//private String scname;         //栏目名称
            values.put("scdescription", secondcolumns.getScdescription());//private String scdescription;  //SEO描述
            values.put("seoTitle", secondcolumns.getSeoTitle());//private String seoTitle;       //SEO标题
            values.put("sckeyWord", secondcolumns.getSckeyWord());//private String sckeyWord;      //SEO关键字
            values.put("addTime", secondcolumns.getAddTime());//private String addTime;     //创建时间
            values.put("orderby", secondcolumns.getOrderby());//private Integer orderby;       //排序顺序
            values.put("modulesId", secondcolumns.getModulesId());//private Integer modulesId;     //栏目所属模块
            values.put("url", secondcolumns.getUrl());//private String url;            //路径
            values.put("urlrewrite", secondcolumns.getUrlrewrite());//private String urlrewrite;     //真实路径
            values.put("rolePower", secondcolumns.getRolePower());//private String rolePower;     //角色权限:1表示普通用户；2表示医生
            values.put("firstColumnName", secondcolumns.getFirstColumnName());//private String firstColumnName;//栏目名称
            result = db.insert(TableName, null, values);
        } catch (Exception e) {
           Log.e("异常1509301858", e.toString());
        } finally {
            return result;
        }
    }

    public void deleteAll()
    {
        SQLiteDatabase db=null;
        try
        {
            db =dataInit.creatOrOpenDatabase();
            db.execSQL("delete from " + TableName + " ;");
//            db.execSQL("update sqlite_sequence SET seq = 0 where name ='"+TableName+"';");
        }
        catch (Exception e)
        {
           Log.e("异常1509301857", e.toString());
        }
        finally
        {
            Log.i("medicationSecondList","deleteAll");
            db.close();
        }
    }

    public List getAll() {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<HashMap<String,String>> list=new ArrayList<>();
        try
        {
            db =dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"scname","modulesId"}, null, null,null,null,null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put("modulesId",cursor.getString(cursor.getColumnIndex("modulesId")));
                hashMap.put("scname",cursor.getString(cursor.getColumnIndex("scname")));
                list.add(hashMap);
            }
        }
        catch (Exception e)
        {
           Log.e("异常1509301859",e.toString());
        }
        finally
        {
            db.close();
            return list;
        }
    }

    public List getSecondByFirstId(String firstColumnId) {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<HashMap<String,String>> list=new ArrayList<>();
        try
        {
            db =dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"scname","id"}, "firstColumnId=?", new String[]{firstColumnId},null,null,null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put("id",cursor.getString(cursor.getColumnIndex("id")));
                hashMap.put("scname",cursor.getString(cursor.getColumnIndex("scname")));
                list.add(hashMap);
            }
        }
        catch (Exception e)
        {
           Log.e("异常",e.toString());
        }
        finally
        {
            db.close();
            return list;
        }
    }

    public void InjectionSQList()
    {
        SQLiteDatabase db = dataInit.creatOrOpenDatabase();
        File file=new File(dataInit.getDataBaseDirectory()+"/"+dataInit.getMedicationSecondListDataFileName());
        try
        {
            BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
            String strline = null;
            while ((strline=bufferedReader.readLine())!=null)
            {
                JSONObject jsonObject=new JSONObject(strline);
                Secondcolumns secondcolumns = new Secondcolumns();
                secondcolumns.setId(jsonObject.getString("id"));//private String id;
                secondcolumns.setFirstColumnId(jsonObject.getLong("firstColumnId"));//private Long firstColumnId;    //关联一级栏目
                secondcolumns.setScname(jsonObject.getString("scname"));//private String scname;         //栏目名称
                secondcolumns.setScdescription(jsonObject.getString("scdescription"));//private String scdescription;  //SEO描述
                secondcolumns.setSeoTitle(jsonObject.getString("seoTitle"));//private String seoTitle;       //SEO标题
                secondcolumns.setSckeyWord(jsonObject.getString("sckeyWord"));//private String sckeyWord;      //SEO关键字
                secondcolumns.setAddTime(jsonObject.getString("addTime"));//private String addTime;     //创建时间
                secondcolumns.setOrderby(jsonObject.getInt("orderby"));//private Integer orderby;       //排序顺序
                secondcolumns.setModulesId(jsonObject.getInt("modulesId"));//private Integer modulesId;     //栏目所属模块
                secondcolumns.setUrl(jsonObject.getString("url"));//private String url;            //路径
                secondcolumns.setUrlrewrite(jsonObject.getString("urlrewrite"));//private String urlrewrite;     //真实路径
                secondcolumns.setRolePower(jsonObject.getString("rolePower"));//private String rolePower;     //角色权限:1表示普通用户；2表示医生
                secondcolumns.setFirstColumnName(jsonObject.getString("firstColumnName"));//private String firstColumnName;//栏目名称
                add(secondcolumns, db);
            }
            Log.i("secondcolumns", "结束");
            bufferedReader.close();
        }
        catch (Exception e)
        {
           Log.e("异常", e.toString());
        }
        finally
        {
            db.close();
        }
    }

}
